//
//  swap.h
//  UnixV7FSChecker
//
//  Created by Bram Buurlage on 7/10/12.
//  Copyright (c) 2012 Bram Buurlage. All rights reserved.
//

#ifndef UnixV7FSChecker_swap_h
#define UnixV7FSChecker_swap_h

template <class T> T swap_endian(T u)
{
    static const short number = 1;
    static const bool machineIsBE = ((byte*)&number)[0] == 0;
    if (machineIsBE) return u;
    
    union
    {
        T u;
        unsigned char u8[sizeof(T)];
    } source, dest;
    
    source.u = u;
    
    for (size_t k = 0; k < sizeof(T); k++)
        dest.u8[k] = source.u8[sizeof(T) - k - 1];
    
    return dest.u;
}

#endif
